package controller

import (
	"Gin/app/service"
	"Gin/app/vo"
	"Gin/config"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type UserController struct {
}

var usrOperate *service.UserService

// Login 登录
func (uc *UserController) Login(c *gin.Context) {
	loginFrom := &vo.LoginVo{}
	err := c.BindJSON(loginFrom)
	if err != nil {
		vo.FAIL(nil, config.ServerBusy, http.StatusOK, c)
		return
	}
	userVo := usrOperate.Login(loginFrom)
	if userVo == nil {
		vo.FAIL(nil, config.LoginFailForUserNameOrPassword, http.StatusOK, c)
		return
	}
	vo.OK(userVo, config.LoginSuccess, http.StatusOK, c)
}

// Register 注册
func (uc *UserController) Register(c *gin.Context) {
	registerFrom := &vo.RegisterVo{}
	err := c.BindJSON(registerFrom)
	if err != nil {
		vo.FAIL(nil, config.ServerBusy, http.StatusInternalServerError, c)
		return
	}
	if registerFrom.Password != registerFrom.RePassword {
		vo.FAIL(nil, config.RegisterFail, http.StatusInternalServerError, c)
		return
	}
	usr, err := usrOperate.Register(registerFrom, c.ClientIP())
	if err == config.UserNameExistError {
		vo.FAIL(nil, config.UserExist, http.StatusInternalServerError, c)
		return
	}
	if usr == nil || err != nil {
		vo.FAIL(nil, config.ServerBusy, http.StatusInternalServerError, c)
		return
	}
	vo.OK(nil, config.RegisterSuccess, http.StatusOK, c)
}

// FindFriend 搜索好友
func (uc *UserController) FindFriend(c *gin.Context) {
	targetUserName := c.Query("username")
	fmt.Println(targetUserName)
	usr, err := usrOperate.SearchFriend(targetUserName)
	if err == config.NoFindTheUser {
		vo.FAIL(nil, config.NoThisUserName, http.StatusOK, c)
		return
	}
	vo.OK(usr, config.FindSuccess, http.StatusOK, c)
}

// SendAddFriend  发送添加好友消息
func (uc *UserController) SendAddFriend(c *gin.Context) {
	fMsg := &vo.AddFriendMessage{}
	_ = c.BindJSON(fMsg)
	usrOperate.SendAddFriendMessage(fMsg)
}

// AddFriend 添加好友
func (uc *UserController) AddFriend(c *gin.Context) {
	from, _ := strconv.Atoi(c.Query("from"))
	target, _ := strconv.Atoi(c.Query("target"))
	flag := usrOperate.AddFriend(from, target)
	if !flag {
		vo.FAIL(nil, "添加失败", http.StatusOK, c)
		return
	}
	vo.OK(nil, "添加成功", http.StatusOK, c)
}

// UnAdd 不添加
func (uc *UserController) UnAdd(c *gin.Context) {
	from, _ := strconv.Atoi(c.Query("from"))
	target, _ := strconv.Atoi(c.Query("target"))
	usrOperate.UnAdd(from, target)
}

// GetChatList 获得历史聊天列表
func (uc *UserController) GetChatList(c *gin.Context) {
	fId, _ := strconv.Atoi(c.Query("id"))
	list := usrOperate.GetChatList(fId)
	vo.OK(list, "ok", http.StatusOK, c)
}

// GetHistoryMessage 获取用户历史聊天信息
func (uc *UserController) GetHistoryMessage(c *gin.Context) {
	fId, _ := strconv.Atoi(c.Query("from"))
	tId, _ := strconv.Atoi(c.Query("target"))
	historyMessage := usrOperate.GetTheHistoryMessage(fId, tId)
	vo.OK(historyMessage, "ok", http.StatusOK, c)
}

// GetFriendList 获取好友列表
func (uc *UserController) GetFriendList(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Query("from"))
	list, err := usrOperate.GetFriendList(userId)
	if err != nil {
		vo.FAIL(nil, "fail", http.StatusOK, c)
		return
	}
	vo.OK(list, "ok", http.StatusOK, c)
}

// GetAddMessage 获取好友添加信息
func (uc *UserController) GetAddMessage(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Query("from"))
	message, length := usrOperate.GetAddFriendMessage(userId)
	vo.OK(message, strconv.Itoa(length), http.StatusOK, c)
}

func (uc *UserController) Acknowledge(c *gin.Context) {
	from, _ := strconv.Atoi(c.Query("from"))
	target, _ := strconv.Atoi(c.Query("target"))
	usrOperate.AcknowledgeMessage(from, target)
}
